﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Configuration;
using System.Reflection;

namespace APC.Common
{
   public class Log
    {

        #region Properties

        public string LogMessage
        {
            set { _LogMessage = value;}
        }
        private string _LogMessage = "";

        public string UserId
        {
            set { _UserId = value ;}
        }
        private string _UserId = "";

       /// <summary>
       /// "Order", "RawMaterial", "Recipe" etc
       /// </summary>
        public string ObjectName
        {
            set { _ObjectName = value; }
        }
        private string _ObjectName = "";

       /// <summary>
       /// Id of object e.g Recipe.Id
       /// </summary>
        public string ObjectId
        {
            set { _ObjectId = value; }
        }
        private string _ObjectId = "";

       /// <summary>
       /// New, Edit, Delete
       /// </summary>
        public string ObjectChangeType
        {
            set { _ObjectChangeType = value; }
        }
        private string _ObjectChangeType = "";

        #endregion

        #region Constructor

        public Log()
        {
          
        }

        #endregion

        #region Methods

       /// <summary>
       /// Contructs the log entrance in the csv file...
       /// </summary>
        public void Write()
        {
            DateTime dt = DateTime.Today;
            StringBuilder str = new StringBuilder();

            string yyyy = dt.Year.ToString();
            string mm = dt.Month.ToString("00");
            string dd = dt.Day.ToString("00");

            string FileName = yyyy + mm + dd + ".csv";
            string path = "";
          
            str.Append(DateTime.Now.ToString());
            str.Append(";");
            str.Append(_UserId);
            str.Append(";");
            str.Append(_ObjectName);
            str.Append(";");
            str.Append(_ObjectId);
            str.Append(";");
            str.Append(_ObjectChangeType);
            str.Append(";");
            str.Append(_LogMessage);

            //Read configuration from another assembly: main project: APC.exe
            Configuration cfg;
            ExeConfigurationFileMap configFile = new ExeConfigurationFileMap();

            try
            {
                configFile.ExeConfigFilename = Path.Combine(Environment.CurrentDirectory, "APC.exe.config");
                cfg = ConfigurationManager.OpenMappedExeConfiguration(configFile, ConfigurationUserLevel.None);
                ConfigurationSectionGroupCollection sectionGroups = cfg.SectionGroups;

                foreach (ConfigurationSectionGroup sectionGroup in sectionGroups)
                {
                    if (sectionGroup.Name == "applicationSettings")
                    {
                        foreach (ConfigurationSection configurationSection in sectionGroup.Sections)
                        {
                            ClientSettingsSection css = ConfigurationManager.GetSection(configurationSection.SectionInformation.SectionName) as ClientSettingsSection;

                            foreach (SettingElement se in css.Settings)
                            {
                                if (se.Name == "LogPath")
                                {
                                    path = se.Value.ValueXml.InnerText;
                                }
                            }
                        }
                    }
                } 
            }
            catch (Exception ex)
            {
            }
            

            try
            {
                StreamWriter log;

                if (!File.Exists(@path + FileName))
                {
                    log = new StreamWriter(@path + FileName);
                }
                else
                {
                    log = File.AppendText(@path + FileName);
                }

                log.WriteLine(str.ToString());
                log.Close();
            }
            catch (Exception ex)
            {

            }
          
        }

        #endregion

    }
}
